package com.jiazhong.IO流.对象流;

import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 对象流
 */
public class ObjectStream {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //objectOutputStreamDemoForList();//输出对象(序列化)
        objectInputStreamDemoForList();
    }

    /**
     * 使用对象输入流进行反序列化操作-从磁盘上读取数据并转换为Java对象
     */
    public static void objectInputStreamDemo() throws IOException, ClassNotFoundException {
        //创建对象输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/user.obj"));
       /* Object obj = null;
        while((obj = objectInputStream.readObject()) != null){
            User user = (User)obj;
            System.out.println(user.getUserId()+","+user.getUsername()+","+user.getPassword());
        }*/

        while(true){
            try{
                User user = (User)objectInputStream.readObject();
                System.out.println(user.getUserId()+","+user.getUsername()+","+user.getPassword());
            }catch (EOFException e){
                System.out.println("读取结束");
                break;
            }

        }
        objectInputStream.close();
    }
    /**
     * 使用对象输出流进行序列化-将Java对象存储到磁盘上
     */
    public static  void objectOutputStreamDemo() throws IOException {

        User user = new User(100,"admin","123456",new Address(1,"陕西省","西安市"));
        User user1 = new User(101,"admin","123456",new Address(1,"陕西省","西安市"));
        User user2 = new User(102,"admin","123456",new Address(1,"陕西省","西安市"));
        User user3 = new User(103,"admin","123456",new Address(1,"陕西省","西安市"));
        /*Type[] genericInterfaces = user.getClass().getGenericInterfaces();
        for (Type type : genericInterfaces){
            System.out.println(type.getTypeName());
        }*/
        //创建对象输出流对象
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/user.obj"));
        objectOutputStream.writeObject(user);
        objectOutputStream.writeObject(user1);
        objectOutputStream.writeObject(user2);
        objectOutputStream.writeObject(user3);
        objectOutputStream.flush();
        objectOutputStream.close();
    }



    public static void objectInputStreamDemoForList() throws IOException, ClassNotFoundException {
        //创建对象输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/users.obj"));
        while(true){
            try{
                List<User> users = (List<User>)objectInputStream.readObject();
                for(User user : users){
                    System.out.println(user.getUserId()+","+user.getUsername()+","+user.getPassword());
                    System.out.println(user.getAddress());
                }

            }catch (EOFException e){
                System.out.println("读取结束");
                break;
            }

        }
        objectInputStream.close();
    }
    /**
     * 使用对象输出流进行序列化-将Java对象存储到磁盘上
     */
    public static  void objectOutputStreamDemoForList() throws IOException {
        List<User> users = new ArrayList<>();
        User user = new User(100,"admin","123456",new Address(1,"陕西省","西安市"));
        User user1 = new User(101,"admin","123456",new Address(1,"陕西省","西安市"));
        User user2 = new User(102,"admin","123456",new Address(1,"陕西省","西安市"));
        User user3 = new User(103,"admin","123456",new Address(1,"陕西省","西安市"));
        users.add(user);
        users.add(user1);
        users.add(user2);
        users.add(user3);

        //创建对象输出流对象
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/users.obj"));
        objectOutputStream.writeObject(users);
        objectOutputStream.flush();
        objectOutputStream.close();
    }
}
